﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Utils;

namespace Etapa1.SofRender
{
    public class TesShader : SEffect,PixelShader,VertexShader,HullShader,DomainShader
    {
        public TesShader()
        {
            this.GeometricShader = null;
            this.PixelShader = this;
            this.VertexShader = this;
            this.DomainShader = this;
            this.HullShader = this;
        }

        #region VertexShader Members

        VertexType VertexShader.VertexShader(VertexType vertice)
        {
            Vector4 pos = vertice[Slots.POSITION0];                        
            VertexType output = new VertexType();
            output[Slots.POSITION0] = pos;            
            return output;
        }

        #endregion

        #region PixelShader Members

        Color PixelShader.PixelShader(VertexType vo)
        {
            return new Color(vo[Slots.COLOR0]);            
        }

        #endregion

        #region DomainShader Members

        public VertexType MainDS(float[] tessFactors, Vector3 BarycentricCoordinatesGenerated, VertexType[] CntPoints)
        {
            VertexType v0 = new VertexType();
            Vector4 p0 = GetCartesian(CntPoints[0], CntPoints[1], CntPoints[2], BarycentricCoordinatesGenerated);
            v0[Slots.POSITION0] = MapToScreen(new Vector4(p0.X, p0.Y, p0.Z, 1));
            v0[Slots.COLOR0] = StaticRandom.RandomColor().ToVector4();
            return v0;
        }

        Vector4 GetCartesian(VertexType p0, VertexType p1, VertexType p2, Vector3 bari)
        {
            return p0[Slots.POSITION0] * bari.X + p1[Slots.POSITION0] * bari.Y + p2[Slots.POSITION0] * bari.Z;
        }


        Vector4 MapToScreen(Vector4 vec)
        {
            return new Vector4((vec.X * 2) - 1, ((1 - vec.Y) * 2) - 1, vec.Z, 1);
        }

        #endregion

        #region HullShader Members

        public float[] ConstantHS(VertexType[] CntPoints, float patchId)
        {
            return new float[] {3,3,3};
        }

        public VertexType MainHS(VertexType[] CntPoints, int curPntId, float patchId)
        {
            return CntPoints[curPntId];
        }

        #endregion
    }
}
